package org.chatboleh.table;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.chatboleh.bean.Payee;

public class PayeeListTable extends AbstractTableModel {

	private static final long serialVersionUID = 5163067677536543780L;
	private String[] columnNames;
	private Object[][] data;

	public PayeeListTable(List<Payee> payeelist) {
		this.columnNames = new String[] { "id", "name", "address", "email",
				"phone", "cdate" };
		int rowcount = 0, colcount = 0;
		rowcount = payeelist.size();
		colcount = this.columnNames.length;
		data = new Object[rowcount][colcount];
		for (int i = 0; i < rowcount; i++) {
			data[i][0] = payeelist.get(i).getId();
			data[i][1] = payeelist.get(i).getName();
			data[i][2] = payeelist.get(i).getAddress();
			data[i][3] = payeelist.get(i).getEmail();
			data[i][4] = payeelist.get(i).getPhone();
			data[i][5] = payeelist.get(i).getCdate();
		}
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int row, int col) {
		return data[row][col];
	}

	public String getColumnName(final int col) {
		return columnNames[col];
	}

	/*
     * JTable uses this method to determine the default renderer/ editor for
     * each cell. If we didn't implement this method, then the last column would
     * contain text ("true"/"false"), rather than a check box.
     */
	public Class<?> getColumnClass(final int c) {
		return getValueAt(0, c).getClass();
	}

	/*
     * Don't need to implement this method unless your table's editable.
     */
	public boolean isCellEditable(final int row, final int col) {
		// Note that the data/cell address is constant,
		// no matter where the cell appears onscreen.
		if (col < 1) {
			return false;
		} else {
			return true;
		}
	}

	/*
     * Don't need to implement this method unless your table's data can change.
     */
	public void setValueAt(final Object value, final int row, final int col) {
		data[row][col] = value;
		fireTableCellUpdated(row, col);
	}
}
